package com.ctgu.controller;


import com.ctgu.entity.Supplier;
import com.ctgu.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    @Qualifier("SupplierServiceImpl")
    private SupplierService supplierService;

    @RequestMapping("allSupplier")
    public String list(Model model){
        List<Supplier> list = supplierService.getAllSupplierList();
        model.addAttribute("list" ,list);
        return "allSupplier";
    }

    @RequestMapping("toAddSupplier")
    public String toAddPage(){
        return "addSupplier";
    }

    @RequestMapping("addSupplier")
    public String addSupplier(Supplier supplier){
        supplierService.addSupplier(supplier);
        return "redirect:allSupplier";
    }

    @RequestMapping("toUpdatePage")
    public String toUpdatePage(int id,Model model){
        Supplier supplier = supplierService.getSupplier(id);
        model.addAttribute("getSupplier",supplier);
        return "updateSupplier";
    }

    @RequestMapping("updateSupplier")
    public String updateSupplier(Supplier supplier){
        supplierService.updateSupplier(supplier);
        return "redirect:allSupplier";
    }

    @RequestMapping("deleteSupplier")
    public String deleteSupplier(int id){
        supplierService.deleteSupplierById(id);
        return "redirect:allSupplier";
    }

    @RequestMapping("querySupplier")
    public String querySupplier(String name,Model model){
        List<Supplier>  list  = supplierService.querySupplierByIdName(name);
        if(list.size()==0){
            list = supplierService.getAllSupplierList();
            model.addAttribute("error","未查到");
        }
        model.addAttribute("list",list);
        return "allSupplier";
    }
}
